package com.ng.controller;

import com.ng.Service.ContentService;
import com.ng.common.utils.FileuploadUtils;
import com.ng.common.utils.PageUtils;
import com.ng.common.utils.R;
import com.ng.entity.ESReqEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cb
 * @Description TODO
 * @date 2023/3/5
 */
@RestController
@RequestMapping("/es")
public class escontroller {

    @Autowired
    private ContentService contentService;

    @Value("${ng.upload.local.esPath:}")
    private String espath ;
    @RequestMapping("/all")
    public void   GetAll() throws IOException {
        contentService.searchall();
    }

    @ResponseBody
    @GetMapping("/search")
    public R<Object> search(ESReqEntity esre) throws IOException
    {
        Map<String,Object> results = new HashMap<>();
        results= contentService.search(esre);

        return R.ok().setData( results);

    }

    @GetMapping("/findbyid")
    public   Object  serachbyid(String index,String id) throws IOException {

        return   contentService.getesbyid(index,id);
    }

    @GetMapping("/selectqi")
    public R<Object> parse(String stage,String pykm,String year ) throws IOException
    {
        Map<String,Object> results = new HashMap<>();
        results= contentService.selectqi( pykm,year,stage);

        return R.ok().setData( results);

    }

    @RequestMapping("/download")
    public  void    filestr(ESReqEntity es, HttpServletResponse response) throws IOException {
        String  filename=es.getFilename();
        String  year=es.getYear();
        String  qi=es.getQi();
        String  gph=es.getGph();
        String  zjh=es.getZjdm().substring(0,1);
        String  pykm=es.getPykm();
        String title=es.getTitle();

            String  newfile=espath+"\\"+zjh+"\\"+gph+"\\"+pykm+year+qi+"\\"+filename+".pdf";
        String  newfile1=espath+"\\"+zjh+"\\"+gph+"\\"+pykm+year+qi+"\\"+filename;
            File file = new File(newfile);
            if(!file.exists()){
                creatbat(newfile,newfile1);
                if(file.exists()){
                    HttpServletResponse download = FileuploadUtils.download(newfile, response,title+".pdf");
                }
            }
            else {
                HttpServletResponse download = FileuploadUtils.download(newfile, response,title+".pdf");
            }

        }




    public void creatbat(String  newfile,String newfile1) throws IOException{

        File bat = new File("E:\\pdfdistillold\\cmd33.bat");
        String inpath=newfile1+".teb";
        String inpath1=newfile1+".caj";
        String inpath2=newfile1+".nh";
        String inpath3=newfile1+".kdh";
        String outpath=newfile;
        OutputStreamWriter osw =new OutputStreamWriter(
                (new FileOutputStream(bat)),"gbk");

        osw.write("E:\\pdfdistillold\\PDFDistill.exe --in "+inpath+" --out "+outpath+" --pdf");
        osw.write("\r\n");
        osw.write("E:\\pdfdistillold\\PDFDistill.exe --in "+inpath1+" --out "+outpath+" --pdf");
        osw.write("\r\n");
        osw.write("E:\\pdfdistillold\\PDFDistill.exe --in "+inpath2+" --out "+outpath+" --pdf");
        osw.write("\r\n");
        osw.write("E:\\pdfdistillold\\PDFDistill.exe --in "+inpath3+" --out "+outpath+" --pdf");
        //osw.write("pause");
        osw.close();
        String ret = excuteCmdCommand("E:\\pdfdistillold\\cmd33.bat");
        System.out.println(ret);
    }

    public static String excuteCmdCommand(String cmdCommand)
    {
        StringBuilder stringBuilder = new StringBuilder();
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(cmdCommand);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), "GBK"));
            String line = null;
            while((line=bufferedReader.readLine()) != null)
            {
                stringBuilder.append(line+"\n");
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
